package com.hongyan.dao;

import com.hongyan.bean.Class;
import com.hongyan.bean.Class;
import com.hongyan.util.JDBCUtils;

import java.util.List;

public class ClassDao {
    public static List<Class> getClassList() {
        List<Class> query = JDBCUtils.query("select * from class", com.hongyan.bean.Class.class);
        return query;
    }

    public static Object[][] findClassByName(String name) {

        String sql = "select * from class where c_name like '%" + name + "%' order by c_name asc;";
        List<Class> query = null;
        query = JDBCUtils.query(sql, Class.class);
        Object[][] Classes = new Object[query.size()][];
        for (int i = 0; i < query.size(); i++) {
            Classes[i] = query.get(i).toArray();
        }
        return Classes;
    }

    public static Class findClassById(String id) {

        String sql = "select * from class where c_id = " + id + "; ";
        List<Class> query = null;
        query = JDBCUtils.query(sql, Class.class);
        Class c = query.get(0);;
        return c;
    }

    public static Object[][] getClassListBySql(String sql) {

        List<Class> query = null;
        query = JDBCUtils.query(sql, Class.class);
        Object[][] Classes = new Object[query.size()][];
        for (int i = 0; i < query.size(); i++) {
            Classes[i] = query.get(i).toArray();
        }
        return Classes;
    }

    public static void deleteClass(String id) {
        String sql = "DELETE FROM class WHERE c_id = ?;";
        JDBCUtils.update(sql, id);

    }


    public static int insertClass(Class c) {
        String sql = "INSERT INTO class(c_id,c_name) VALUES(?,?);";
        return JDBCUtils.update(sql, c.getC_id(),c.getC_name());
    }

    public static int updateClass(Class c) {
        String sql = "UPDATE class SET c_name = ? WHERE c_id = ?;";;
        return JDBCUtils.update(sql,c.getC_name(), c.getC_id());
    }
}